package com.dcliu.db;

import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Page<T extends Object> implements Serializable {

  private static final long serialVersionUID = 1L;

  /**
   * 当前页
   */
  private int page = 1;

  /**
   * 每页记录数
   */
  private int size = 15;

  /**
   * 总记录数
   */
  private int count;

  /**
   * 结果集
   */
  @SuppressWarnings("unchecked")
  private List<T> result = Collections.EMPTY_LIST;

  /**
   * 参数
   */
  private Map<String, Object> params = new HashMap<String, Object>();

  /**
   * 分页的开始
   */
  public int getStartNum() {
    if (page > 1)
      return (page - 1) * size;
    return 0;
  }

  /**
   * 分页的结束
   */
  public int getEndNum() {
    if (page < this.getPageCount())
      return (page + 1) * size - 1;
    return count - 1;
  }

  /**
   * 总页数
   */
  public int getPageCount() {
    if (count > 0) {
      int pageCount = count / size;
      if(count % size == 0) {
        return pageCount;
      } else {
        return pageCount + 1;
      }
    }
    return 0;
  }

  public int getPage() {
    return page;
  }

  public void setPage(int page) {
    this.page = page <= 0 ? 1 : page;
  }

  public void setPage(Object page) {
    try {
      if (page == null)
        this.page = 1;
      else {
        int _page = Integer.parseInt(page.toString());
        this.page = _page <= 0 ? 1 : _page;
      }
    } catch (Exception e) {
      this.page = 1;
    }
  }

  public int getSize() {
    return size;
  }

  public void setSize(int size) {
    this.size = size <= 0 ? 1 : size;
  }

  public void setSize(Object size) {
    try {
      if (size == null)
        this.size = 20;
      else {
        int _size = Integer.parseInt(size.toString());
        this.size = _size <= 0 ? 1 : _size;
      }
    } catch (Exception e) {
      this.size = 20;
    }
  }

  public int getCount() {
    return count;
  }

  public void setCount(int count) {
    this.count = count;
  }

  public List<T> getResult() {
    return result;
  }

  public void setResult(List<T> result) {
    this.result = result;
  }

  public Map<String, Object> getParams() {
    return params;
  }

  public void setParams(Map<String, Object> params) {
    this.params = params;
  }
}